﻿package mortal.component.window
{
    import com.gengine.debug.*;
    import com.gengine.resource.*;
    import com.gengine.utils.pools.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import com.mui.utils.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.*;
    import flash.utils.*;
    import frEngine.pools.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.view.common.*;

    public class DebugWindow extends BaseWindow implements ILog
    {
        private var _tabBar:GTabBar;
        private var _textAreaPrint:GTextArea;
        private var _textAreaMsgCode:GTextArea;
        private var _textAreaError:GTextArea;
        private var _textAreaOther:GTextArea;
        private var _textAreaPool:GTextArea;
        private var _textAreaInfo:GTextArea;
        private var _clearBtn:GButton;
        private var _clearMsg:GButton;
        private var _textInput:GTextInput;
        private var _setOnlyButton:GButton;
        private var _copyBtn:GButton;
        private var _clearOther:GButton;
        private var _refreshPool:GButton;
        private var _clearPool:GButton;
        private var _refreshInfo:GButton;
        private var _clearInfo:GButton;
        public var onlyMsgCode:int;
        private var _otherStr:String = "";
        private var _codeStr:String = "";
        private var _textStr:String = "";
        private var _errorStr:String = "";
        private var _laseErrorStr:String;
        private static var _instance:DebugWindow;

        public function DebugWindow()
        {
            if (_instance)
            {
                throw new Error("DebugWindow 单例");
            }
            titleHeight = 55;
            this.setSize(500, 500);
            title = "提示";
            return;
        }// end function

        override protected function configUI() : void
        {
            super.configUI();
            this._tabBar = UIFactory.gTabBar(20, 32, [{label:"调试信息", name:"print"}, {label:"收到消息", name:"pool"}, {label:"错误信息", name:"error"}, {label:"其他调试", name:"other"}, {label:"对象池", name:"pool"}, {label:"info", name:"info"}], 70, 22, this, this.onTabBarChange);
            UIFactory.bg(16, 64, 469, 397, this);
            this._textAreaPrint = UIFactory.textArea("", 20, 68, 453, 380, this);
            this._textAreaError = UIFactory.textArea("", 20, 68, 453, 380, this);
            this._textAreaError.visible = false;
            this._textAreaMsgCode = UIFactory.textArea("", 20, 68, 453, 380, this);
            this._textAreaMsgCode.visible = false;
            this._textAreaOther = UIFactory.textArea("", 20, 68, 453, 380, this);
            this._textAreaOther.visible = false;
            this._textAreaPool = UIFactory.textArea("", 20, 68, 453, 380, this);
            this._textAreaPool.visible = false;
            this._textAreaInfo = UIFactory.textArea("", 20, 68, 453, 380, this);
            this._textAreaInfo.visible = false;
            this._clearBtn = UIFactory.gButton("清除内容", 387, 463, 70, 22, this);
            this._clearBtn.addEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._textInput = UIFactory.gTextInput(200, 463, 90, 20, this);
            this._textInput.visible = false;
            this._setOnlyButton = UIFactory.gButton("过滤消息", 300, 463, 70, 22, this);
            this._setOnlyButton.visible = false;
            this._setOnlyButton.addEventListener(MouseEvent.CLICK, this.onSetMsgOnlyHandler);
            this._clearMsg = UIFactory.gButton("清理消息", 387, 463, 70, 22, this);
            this._clearMsg.visible = false;
            this._clearMsg.addEventListener(MouseEvent.CLICK, this.onClearMsgHandler);
            this._copyBtn = UIFactory.gButton("复制错误", 387, 463, 70, 22, this);
            this._copyBtn.visible = false;
            this._copyBtn.addEventListener(MouseEvent.CLICK, this.onCopyErrorHandler);
            this._clearOther = UIFactory.gButton("清除内容", 387, 463, 70, 22, this);
            this._clearOther.visible = false;
            this._clearOther.addEventListener(MouseEvent.CLICK, this.onClearOtherHandler);
            this._refreshPool = UIFactory.gButton("刷新池子", 287, 463, 70, 22, this);
            this._refreshPool.visible = false;
            this._refreshPool.addEventListener(MouseEvent.CLICK, this.updatePoolHander);
            this._clearPool = UIFactory.gButton("清理池子", 387, 463, 70, 22, this);
            this._clearPool.visible = false;
            this._clearPool.addEventListener(MouseEvent.CLICK, this.onClearPoolHandler);
            this._refreshInfo = UIFactory.gButton("刷新Info", 287, 463, 70, 22, this);
            this._refreshInfo.visible = false;
            this._refreshInfo.addEventListener(MouseEvent.CLICK, this.updateInfoHander);
            this._clearInfo = UIFactory.gButton("保存Info", 387, 463, 70, 22, this);
            this._clearInfo.visible = false;
            this._clearInfo.addEventListener(MouseEvent.CLICK, this.onSaveInfoHandler);
            this.addChild(this._clearBtn);
            this.addEventListener(WindowEvent.SHOW, this.onShow);
            return;
        }// end function

        private function onShow(event:WindowEvent) : void
        {
            this.changToTabBar(this._tabBar.selectedIndex);
            return;
        }// end function

        private function onTabBarChange(event:MuiEvent) : void
        {
            this.changToTabBar(event.selectedIndex);
            return;
        }// end function

        public function changToTabBar(param1:int) : void
        {
            if (param1 >= 0 && param1 < this._tabBar.dataProvider.length)
            {
                this.hideAllPanel();
                this._tabBar.selectedIndex = param1;
                if (param1 == 0)
                {
                    this._textAreaPrint.visible = true;
                    this._clearBtn.visible = true;
                    this.updatePrintMsg();
                }
                if (param1 == 1)
                {
                    this._textAreaMsgCode.visible = true;
                    this._clearMsg.visible = true;
                    this._textInput.visible = true;
                    this._setOnlyButton.visible = true;
                    this.updateCodeMsg();
                }
                if (param1 == 2)
                {
                    this._textAreaError.visible = true;
                    this._copyBtn.visible = true;
                    this.updateErrorMsg();
                }
                if (param1 == 3)
                {
                    this._textAreaOther.visible = true;
                    this._clearOther.visible = true;
                    this.updateOtherMsg();
                }
                if (param1 == 4)
                {
                    this._textAreaPool.visible = true;
                    this._refreshPool.visible = true;
                    this._clearPool.visible = true;
                    this.updatePoolMsg();
                }
                if (param1 == 5)
                {
                    this._textAreaInfo.visible = true;
                    this._refreshInfo.visible = true;
                    this._clearInfo.visible = true;
                    this.updateInfoMsg();
                }
            }
            return;
        }// end function

        private function hideAllPanel() : void
        {
            this._textAreaPrint.visible = false;
            this._textAreaMsgCode.visible = false;
            this._textAreaError.visible = false;
            this._textAreaOther.visible = false;
            this._textAreaPool.visible = false;
            this._textAreaInfo.visible = false;
            this._clearBtn.visible = false;
            this._clearMsg.visible = false;
            this._textInput.visible = false;
            this._setOnlyButton.visible = false;
            this._copyBtn.visible = false;
            this._clearOther.visible = false;
            this._refreshPool.visible = false;
            this._clearPool.visible = false;
            this._refreshInfo.visible = false;
            this._clearInfo.visible = false;
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            this._textAreaPrint.text = "";
            this._textStr = "";
            return;
        }// end function

        private function onCopyErrorHandler(event:MouseEvent) : void
        {
            if (this._laseErrorStr)
            {
                System.setClipboard(this._laseErrorStr);
            }
            return;
        }// end function

        private function onSetMsgOnlyHandler(event:MouseEvent) : void
        {
            this.onlyMsgCode = int(this._textInput.text);
            return;
        }// end function

        private function onClearMsgHandler(event:MouseEvent) : void
        {
            this._textAreaMsgCode.text = "";
            this._codeStr = "";
            return;
        }// end function

        private function onClearOtherHandler(event:MouseEvent) : void
        {
            this._textAreaOther.text = "";
            this._otherStr = "";
            return;
        }// end function

        private function updatePoolHander(event:MouseEvent) : void
        {
            this.updatePoolMsg();
            return;
        }// end function

        private function onClearPoolHandler(event:MouseEvent) : void
        {
            ObjectPool.clearPool();
            UICompomentPool.clearPool();
            this.updatePoolMsg();
            return;
        }// end function

        private function updateInfoHander(event:MouseEvent) : void
        {
            this.updateInfoMsg();
            return;
        }// end function

        private function onSaveInfoHandler(event:MouseEvent) : void
        {
            var _loc_2:* = new ByteArray();
            _loc_2.endian = Endian.LITTLE_ENDIAN;
            var _loc_3:* = new FileReference();
            var _loc_4:String = "";
            _loc_2.writeUTFBytes(ResourceManager.getStr("\r\n"));
            _loc_3.save(_loc_2, "notDisposeInfo.txt");
            return;
        }// end function

        public function debugMsg(param1:int, ... args) : void
        {
            if (param1 == LogType.code)
            {
                this._codeStr = this._codeStr + (args.toString() + "\n");
                if (this._codeStr.length > 10000)
                {
                    this._codeStr = this._codeStr.substr(this._codeStr.length - 10000);
                }
                if (!this.isHide && this._textAreaMsgCode.visible)
                {
                    if (this._codeStr.length > 1000)
                    {
                        this._codeStr = this._codeStr.substr(this._codeStr.length - 1000);
                    }
                    this.updateCodeMsg();
                }
            }
            else if (param1 == LogType.other)
            {
                this._otherStr = this._otherStr + (args.toString() + "\n");
                if (this._otherStr.length > 10000)
                {
                    this._otherStr = this._otherStr.substr(this._otherStr.length - 10000);
                }
                if (!this.isHide && this._textAreaOther.visible)
                {
                    if (this._codeStr.length > 1000)
                    {
                        this._codeStr = this._codeStr.substr(this._codeStr.length - 1000);
                    }
                    this.updateOtherMsg();
                }
            }
            return;
        }// end function

        private function updateCodeMsg() : void
        {
            var _loc_1:* = this._textAreaMsgCode.verticalScrollPosition == this._textAreaMsgCode.maxVerticalScrollPosition;
            this._textAreaMsgCode.text = this._codeStr;
            if (_loc_1)
            {
                this._textAreaMsgCode.verticalScrollPosition = this._textAreaMsgCode.maxVerticalScrollPosition;
            }
            return;
        }// end function

        private function updateOtherMsg() : void
        {
            var _loc_1:* = this._textAreaOther.verticalScrollPosition == this._textAreaOther.maxVerticalScrollPosition;
            this._textAreaOther.text = this._otherStr;
            if (_loc_1)
            {
                this._textAreaOther.verticalScrollPosition = this._textAreaOther.maxVerticalScrollPosition;
            }
            return;
        }// end function

        private function updatePoolMsg() : void
        {
            var _loc_1:* = this._textAreaPool.verticalScrollPosition;
            var _loc_2:* = "ObjectPool:\n" + ObjectPool.getPoolMsg() + "\n\n\nUICompomentPool:\n" + UICompomentPool.getPoolMsg() + "\n\n\nEffectPlayerPool:\n" + EffectPlayerPool.instance.getPoolMsg() + "\n\n\nFrObjectPool:\n" + FrObjectPool.getPoolMsg();
            this._textAreaPool.htmlText = _loc_2;
            this._textAreaPool.verticalScrollPosition = _loc_1;
            return;
        }// end function

        private function updateInfoMsg() : void
        {
            var _loc_1:* = this._textAreaInfo.verticalScrollPosition;
            this._textAreaInfo.htmlText = ResourceManager.getStr("\n");
            this._textAreaInfo.verticalScrollPosition = _loc_1;
            return;
        }// end function

        public function print(... args) : void
        {
            this._textStr = this._textStr + (args.toString() + "\n");
            if (this._textStr.length > 5000)
            {
                this._textStr = this._textStr.substr(this._textStr.length - 5000);
            }
            if (!this.isHide)
            {
                this.updatePrintMsg();
            }
            return;
        }// end function

        private function updatePrintMsg() : void
        {
            var _loc_1:* = this._textAreaPrint.verticalScrollPosition == this._textAreaPrint.maxVerticalScrollPosition;
            this._textAreaPrint.text = this._textStr;
            if (_loc_1)
            {
                this._textAreaPrint.verticalScrollPosition = this._textAreaPrint.maxVerticalScrollPosition;
            }
            return;
        }// end function

        public function error(... args) : void
        {
            args = args.toString();
            if (args == this._laseErrorStr)
            {
                return;
            }
            this._laseErrorStr = args;
            this._errorStr = this._errorStr + (args + "\n\n\n");
            if (this._errorStr.length > 55000)
            {
                this._errorStr = this._errorStr.substr(3000);
            }
            if (!this.isHide)
            {
                this.updateErrorMsg();
            }
            return;
        }// end function

        private function updateErrorMsg() : void
        {
            var _loc_1:* = this._textAreaError.verticalScrollPosition == this._textAreaError.maxVerticalScrollPosition;
            this._textAreaError.text = this._errorStr;
            if (_loc_1)
            {
                this._textAreaError.verticalScrollPosition = this._textAreaError.maxVerticalScrollPosition;
            }
            return;
        }// end function

        public static function get instance() : DebugWindow
        {
            if (_instance == null)
            {
                _instance = new DebugWindow;
            }
            return _instance;
        }// end function

    }
}
